package com.neuedu.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.neuedu.dao.IUserDAO;
import com.neuedu.entity.User;
import com.neuedu.utils.DruidDataSourceUtil;

public class UserDAOImpl implements IUserDAO{

	@Override
	public User getByName(String uname) {
		Connection conn=null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		User user=new User();
		try {
		conn=DruidDataSourceUtil.getConnection();
		String sql="select * from t_user where uname=?";
		
			pstm=conn.prepareStatement(sql);
			
			pstm.setObject(1, uname);
			rs=pstm.executeQuery();
			while(rs.next()) {
				user.setUid(rs.getInt("uid"));
				user.setUname(rs.getString("uname"));
				user.setUpwd(rs.getString("upwd"));
				user.setUlevel(rs.getInt("ulevel"));
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				conn.close();
				pstm.close();
				rs.close();
			}catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(user.getUid()==null||user.getUname()==null||user.getUpwd()==null) {
			return null;
		}
		return user;
	}

	@Override
	public User getByNamePassword(String uname, String upwd) {
		Connection conn=null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		User user=new User();
		try {
		conn=DruidDataSourceUtil.getConnection();
		String sql="select * from t_user where uname=? and upwd=?";
		
			pstm=conn.prepareStatement(sql);
			
			pstm.setObject(1, uname);
			pstm.setObject(2, upwd);
			
			rs=pstm.executeQuery();
			while(rs.next()) {
				user.setUid(rs.getInt("uid"));
				user.setUname(rs.getString("uname"));
				user.setUpwd(rs.getString("upwd"));
				user.setUlevel(rs.getInt("ulevel"));
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				conn.close();
				pstm.close();
				rs.close();
			}catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(user.getUid()==null||user.getUname()==null||user.getUpwd()==null) {
			return null;
		}
		return user;
	}

	
}
